package com.xiaobai.easyexcel.dynamiccolumn;

import cn.hutool.core.date.DateTime;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author wangtw
 * @ClassName DataConverter
 * @description: 类型转换器
 * @date 2024/2/919:23
 */
public class DataConverter {

    public static class CoreConverter<T> implements Converter<T> {

        private final Class<T> tClass;

        public CoreConverter(Class<T> tClass) {
            this.tClass = tClass;
        }

        /**
         * 导出支持的类型
         * @return
         */
        @Override
        public Class supportJavaTypeKey() {
            return tClass;
        }

        /**
         * 导入支持的Excel类型
         * @return
         */
        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return CellDataTypeEnum.STRING;
        }

        /**
         * 导入类型转换
         * @param cellData
         * @param excelContentProperty
         * @param globalConfiguration
         * @return
         * @throws Exception
         */
        @Override
        public T convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
            if (cellData.getData() instanceof LocalDate) {
                return (T) LocalDate.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            }
            if (cellData.getData() instanceof LocalTime) {
                return (T) LocalTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("HH:mm:ss"));
            }
            if (cellData.getData() instanceof LocalDateTime) {
                return (T) LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            }
            if (cellData.getData() instanceof Integer) {
                return (T) Integer.valueOf(cellData.getStringValue());
            }
            return null;
        }

        /**
         * 导出类型转换
         * @param obj
         * @param excelContentProperty
         * @param globalConfiguration
         * @return
         * @throws Exception
         */
        @Override
        public CellData convertToExcelData(T obj, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {

            if (obj instanceof LocalDate) {
                return new CellData(((LocalDate) obj).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            }

            if (obj instanceof LocalDateTime) {
                return new CellData(((LocalDateTime) obj).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }

            if (obj instanceof LocalTime) {
                return new CellData(((LocalTime) obj).format(DateTimeFormatter.ofPattern("HH:mm:ss")));
            }

            if (obj instanceof Integer) {
                return new CellData(String.valueOf(obj));
            }

            return null;
        }
    }

    /**
     * localDate类型转换
     */
    public static class LocalDateConverter extends CoreConverter<LocalDate> {

        public LocalDateConverter() {
            super(LocalDate.class);
        }
    }

    /**
     * localTime类型转换
     */
    public static class LocalTimeConverter extends CoreConverter<LocalTime> {

        public LocalTimeConverter() {
            super(LocalTime.class);
        }
    }

    /**
     * LocalDateTime类型转换
     */
    public static class LocalDateTimeConverter extends CoreConverter<LocalDateTime> {

        public LocalDateTimeConverter() {
            super(LocalDateTime.class);
        }
    }

    /**
     * Integer
     */
    public static class IntegerConverter extends CoreConverter<Integer> {

        public IntegerConverter() {
            super(Integer.class);
        }
    }
}
